rm(list = ls())
options(warn=-1)
library(rgl)
library(misc3d)
knitr::knit_hooks$set(webgl = hook_webgl)

Load plot3D functions.

source("./plot3D_func.R")

STitch3D identifies anatomical regions in the developing human heart.

clusters <- c(0,1,2,3,4)
cluster_colors <- c("#7570b3","#1b9e77","#d95f02","#66a61e","#e7298a")
um <- c(-0.8227578, -0.5278972, 0.2107004, 0,
        -0.5608635, 0.8141541, -0.1502849, 0,
        -0.0922074, -0.2418221, -0.9659296, 0,
        0, 0, 0, 1) #set the initial view of the 3D plot
spot_radius <- 0.3
axis_rescale <- c(-1,1,2)

plot3D_clusters(directory = "./results_6PCW",
                clusters = clusters,
                cluster_colors = cluster_colors,
                um = um,
                spot_radius = spot_radius,
                axis_rescale = axis_rescale
                )
#the heart model is downloaded from https://github.com/MickanAsp/Developmental_heart
load('./Developmental_heart-master/data/ST_heart_spot_data.RData')
load('./Developmental_heart-master/data/heart.RData')

cluster_df <- read.table(paste0("./results_6PCW/clustering_result.csv"), sep=",", header=TRUE)
cluster_df$idx <- unlist(lapply(strsplit(cluster_df$X, "-"), function(x){x[1]}))
cluster_df$sam <- unlist(lapply(strsplit(cluster_df$X, "-"), function(x){substr(x[2],6,7)}))
cluster_df$sam <- as.integer(cluster_df$sam) + 1

atlas <- heart$atlas
atlas$idx <- atlas$spot.pos
atlas$sam <- unlist(lapply(strsplit(atlas$image, "_"), function(x){x[1]}))
atlas$sam <- as.integer(atlas$sam)

spots.table <- merge(cluster_df, atlas, by=c("idx","sam"))
radius.of.spots.in.atlas.pixels<- (100/(2383.36/532))/3

open3d(windowRect = c(0, 0, 720, 720))
## glX 
##   3
par3d(persp)
## NULL
view3d(userMatrix = structure(c(-0.0108720660209656, 0.899227440357208, 
                                0.437346190214157, 0, 0.955604612827301, -0.119448974728584, 
                                0.269354522228241, 0, 0.2944515645504, 0.420858442783356, 
                                -0.858007192611694, 0, 0, 0, 0, 1), .Dim = c(4L, 4L)))
drawScene.rgl(organ.dwnsmp[which(names(organ.dwnsmp)%in%c('WH'))])
spheres3d(598-spots.table$rostral.caudal, 532-spots.table$right.left, spots.table$anterior.posterior, 
          col=cluster_colors[spots.table$louvain+1], radius=radius.of.spots.in.atlas.pixels, alpha=1)